package com.jerry.mingxing.widget;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jerry.mingxing.R;
import com.jerry.mingxing.bean.StoreBean;
import com.jerry.mingxing.ui.store.ContentViewPagerAdapter;
import com.jerry.mingxing.ui.store.RecyclerBrandAdapter;
import com.jerry.mingxing.ui.store.RecyclerShowAdapter;

/**
 * Created by Administrator on 2016/9/1.
 */
public class MyContentView extends LinearLayout {
    private Context context=getContext();
    private final static int TIME=3500;
    private ContentViewPagerAdapter contentViewPagerAdapter;
    private RecyclerBrandAdapter recyclerBrandAdapter;
    private RecyclerShowAdapter recyclerShowAdapter;
    private RecyclerView myview_rlv_brand;
    private RecyclerView myview_rlv_show;
    private ViewPager myview_viewpager;
    private SimpleDraweeView myview_sdv;
    private MyContentViewInter myContentViewInter;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    int currentItem = myview_viewpager.getCurrentItem();
                    myview_viewpager.setCurrentItem(++currentItem);

                    break;
            }
        }
    };


    public MyContentView(Context context) {
        super(context);
        initView();
    }

    public MyContentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyContentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        View View = LayoutInflater.from(context).inflate(R.layout.mycontentview, this);
        myview_rlv_brand = (RecyclerView) findViewById(R.id.myview_rlv_brand);
        myview_rlv_show = (RecyclerView) findViewById(R.id.myview_rlv_show);
        myview_viewpager = (ViewPager) findViewById(R.id.myview_viewpager);
        myview_sdv = (SimpleDraweeView) findViewById(R.id.myview_sdv);


     }

    public void pullData(StoreBean bean){

        contentViewPagerAdapter=new ContentViewPagerAdapter(context,bean.getData().getRegion_pictures());
        recyclerBrandAdapter=new RecyclerBrandAdapter(bean.getData().getRegion_brands(),context);
        recyclerShowAdapter=new RecyclerShowAdapter(bean.getData().getRegion_skus(),context);

        myview_sdv.setImageURI(Uri.parse(bean.getData().getRegion_name().get(0).getComponent().getPicUrl()));
        myview_rlv_brand.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager
                .HORIZONTAL, false));
        myview_rlv_show.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager
                .HORIZONTAL, false));
        myview_rlv_brand.setItemAnimator(new DefaultItemAnimator());
        myview_rlv_show.setItemAnimator(new DefaultItemAnimator());
        myview_rlv_brand.setAdapter(recyclerBrandAdapter);
        myview_rlv_show.setAdapter(recyclerShowAdapter);
        myview_viewpager.setAdapter(contentViewPagerAdapter);

        final MyThread myThread = new MyThread();
        myThread.start();
        myview_viewpager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        myThread.stopRun();
                    break;

                    case MotionEvent.ACTION_UP:

                        myThread.startRun();
                    break;
                }
                return false;
            }
        });

    }
    class MyThread extends Thread{
        private boolean isflag;
        public void stopRun(){
            if (isflag){
                isflag=false;
                handler.removeCallbacks(this);
            }
        }
        public void startRun(){
            if (!isflag){
                handler.removeCallbacks(this);
                isflag = true;
                handler.postDelayed(this, TIME);
            }

        }
        @Override
        public void run() {
            handler.sendEmptyMessage(0);
            handler.postDelayed(this,TIME);
        }
    }
    public void setInter(){

        contentViewPagerAdapter.setOnItemClick(new ContentViewPagerAdapter.MyContentInter() {
            @Override
            public void onItemClick(View view, StoreBean.DataBean.RegionPicturesBean bean) {
                if(myContentViewInter!=null){
                    myContentViewInter.setViewPager(view,bean);
                }
            }
        });
        recyclerShowAdapter.setOnItemClickListener(new RecyclerShowAdapter.MyShowInter() {
            @Override
            public void onClick(View view, StoreBean.DataBean.RegionSkusBean bean) {
                if(myContentViewInter!=null){
                    myContentViewInter.setRecyclerViewShow(view,bean);
                }
            }
        });
        recyclerBrandAdapter.setonMyClickListener(new RecyclerBrandAdapter.MyInter() {
            @Override
            public void onItemClick(View view, StoreBean.DataBean.RegionBrandsBean bean) {
                if(myContentViewInter!=null){
                    myContentViewInter.setRecyclerViewBrand(view,bean);
                }
            }
        });
        myview_sdv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
    public interface MyContentViewInter{
        void setViewPager(View view, StoreBean.DataBean.RegionPicturesBean bean);
        void setRecyclerViewBrand(View view, StoreBean.DataBean.RegionBrandsBean bean);
        void setRecyclerViewShow(View view, StoreBean.DataBean.RegionSkusBean bean);

    }
    public void setOnClick( MyContentViewInter myContentViewInter){
        this.myContentViewInter=myContentViewInter;
    }


}
